package com.jss.lanouhn.fristapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.MyFragmentPager;
import com.jss.lanouhn.fristapp.fragments.HomeSmallFreeFragmentOther;
import com.jss.lanouhn.fristapp.fragments.HomeSmallFreeFragmentToday;

import java.util.ArrayList;
import java.util.List;


public class HomeSelectSmallFreeActivity extends AppCompatActivity {
    private RadioButton radioButtonLeft,radioButtonRight;
    private List<Fragment> fragments=new ArrayList<>();
    private MyFragmentPager adapter;
    private ViewPager viewPager;
    private final int[] array=new int[]{R.id.rb_home_free_left,R.id.rb_home_free_rigth};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_select_small_free);
        fragments.add(new HomeSmallFreeFragmentToday());
        fragments.add(new HomeSmallFreeFragmentOther());

        viewPager = (ViewPager) findViewById(R.id.vp_free);
        radioButtonLeft = (RadioButton) findViewById(R.id.rb_home_free_left);
        radioButtonRight = (RadioButton) findViewById(R.id.rb_home_free_rigth);
        adapter=new MyFragmentPager(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        //viewPager.setOffscreenPageLimit(2);
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(changeListener);

        RadioGroup radioGroup= (RadioGroup) findViewById(R.id.rg_home_free_select);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home_free_left:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_home_free_rigth:
                        viewPager.setCurrentItem(1);
                        break;
                }
            }
        });

    }
    ViewPager.SimpleOnPageChangeListener changeListener=new ViewPager.SimpleOnPageChangeListener(){
        public void onPageSelected(int position){
            change(array[position]);
        }
    };
    private void change(int checkedId){

        switch (checkedId){
            case R.id.rb_home_free_left:
                radioButtonLeft.setChecked(true);
                break;
            case R.id.rb_home_free_rigth:
                radioButtonRight.setChecked(true);
                break;
        }
    }
}
